package com.lanou.pianke.admin.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.pianke.article.entity.Article;
import com.lanou.pianke.articlecomment.entity.ArticleComment;
import com.lanou.pianke.admin.service.AreplyService;
import com.lanou.pianke.admin.service.ArticleCommentService;
import com.lanou.pianke.admin.service.ArticleService;
import com.lanou.pianke.common.entity.PiankeResult;
import com.lanou.pianke.common.utils.PiankeResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCommentService articleCommentService;

    @Autowired
    private AreplyService areplyService;

    //删除举报文章
    @DeleteMapping("/deleteArticle/{contentId}")
    public String deleteArticle(@PathVariable String contentId){
        //删除举报文章
        articleService.deleteByContentId(contentId);
        //通过contentId查询文章里的所有评论
        List<ArticleComment> commentList = articleCommentService.selectIdByContentId(contentId);
        for (ArticleComment articleComment : commentList) {
            //通过id查询评论里的所有回复并删除
            String id = articleComment.getAcomid();
            areplyService.deleteById(id);
        }
        //删除举报文章的评论
        articleCommentService.deleteByContentId(contentId);

        return "delete successfully";
    }

//    //查看所有文章
//    @GetMapping("/showArticles")
//    public List showArticles(){
//        List<Article> list = articleService.showAll();
//        return list;
//    }

    //调用文章项目查看被举报文章
    @GetMapping("/showArticleList")
    public PiankeResult showArticles(){
        List<Article> articles = articleService.showAll();
        return PiankeResultUtils.success(articles);
    }


}
